package com.situ.edu.controller;

import com.situ.edu.pojo.entity.Teacher;
import com.situ.edu.pojo.entity.Xi;
import com.situ.edu.pojo.query.TeacherQuery;
import com.situ.edu.service.IXiTeacherService;
import com.situ.edu.service.IXiService;
import com.situ.edu.service.impl.XiTeacherServiceImpl;
import com.situ.edu.service.impl.XiServiceImpl;
import com.situ.edu.util.MyBeanUtils;
import com.situ.edu.util.PageInfo;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/xiTeacher")
public class XiTeacherController extends BaseController{
    private IXiTeacherService teacherService = new XiTeacherServiceImpl();
    private IXiService xiService = new XiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage"://查询教师信息：请假
                selectByPage(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
            case "add"://添加教师信息
                add(req,resp);
                break;
            case "deleteById": //单个删除
                deleteById(req,resp);
                break;
            case "deleteAll": //批量删除
                deleteAll(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;

        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.update");
        Teacher teacher = MyBeanUtils.copyToBean(req,Teacher.class);
        teacherService.update(teacher);
        toJSON(resp,Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.selectById");
        String id = req.getParameter("id");
        Teacher teacher = teacherService.selectById(Integer.parseInt(id));
        List<Xi> list = xiService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("teacher",teacher);
        map.put("list",list);
        toJSON(resp,Result.ok(map));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        teacherService.deleteAll(ids);
        toJSON(resp,Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.deleteById");
        String id = req.getParameter("id");
        teacherService.deleteById(Integer.parseInt(id));
        toJSON(resp,Result.ok("删除成功"));
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        teacherService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));
        toJSON(resp,Result.ok("更新成功"));
    }


    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.add");
        Teacher teacher = MyBeanUtils.copyToBean(req,Teacher.class);
        teacherService.add(teacher);
        toJSON(resp, Result.ok("添加成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiTeacherController.selectByPage");
        TeacherQuery teacherQuery = MyBeanUtils.copyToBean(req,TeacherQuery.class);
        PageInfo pageInfo = teacherService.selectByPage(teacherQuery);
        toJSON(resp,pageInfo);
    }


}
